package ca.etsmtl.log730.projet.controller.client;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class MainClient {

	private static MainClient instance = null;
	
	public static MainClient getInstance() {
		if(instance == null) instance = new MainClient();
		return instance;
	}
//	protected List<ClientControllerHandler> client = new ArrayList<ClientControllerHandler>();
//	public void addConnection(ClientControllerHandler cch) {
//		client.add(cch);
//		Logger.getLogger("CLIENT").info("NEW CLIENT " + cch.getSessionId().toString());
//	}
//	
//	public void removeConnection(ClientControllerHandler cch) {
//		client.remove(cch);
//		Logger.getLogger("CLIENT").info("CLIENT CLOSED " + cch.getSessionId().toString());
//	}

	private MainClient() {
		
	}
	
	private ClientSetup clientSetup = new ClientSetup();
	private NioSocketConnector connector = new NioSocketConnector();
	public NioSocketConnector getConnector() {
		return connector;
	}

	public void connect() {
				InetAddress HOSTNAME = null;
				try {
					HOSTNAME = InetAddress.getByName(getClientSetup().getHostname());
				} catch (UnknownHostException e2) {
					e2.printStackTrace();
				}
				
				ObjectSerializationCodecFactory oscf = new ObjectSerializationCodecFactory();
				oscf.setDecoderMaxObjectSize(1048576);
				oscf.setEncoderMaxObjectSize(1048576);
				// Configure the service.
				connector.setConnectTimeoutMillis(2000);
				connector.getFilterChain().addLast("codec",
						new ProtocolCodecFilter(oscf));
				connector.getFilterChain().addLast("logger", new LoggingFilter());
				ClientControllerHandler handler = new ClientControllerHandler();
				connector.setHandler(handler);
				IoSession session;
					try {
						ConnectFuture future = connector.connect(new InetSocketAddress(HOSTNAME, getClientSetup().getPort()));
						System.out.print ("Connexion...");
//						future.
						future.awaitUninterruptibly();
						session = future.getSession();
						System.out.println("ÉTABLIE!");
						handler.setSession(session);
						session.getCloseFuture().awaitUninterruptibly();
						connector.dispose();
//						return handler;
					} catch (RuntimeIoException e) {
						System.err.println("Impossible de se connecter!");
						try {
							Thread.sleep(5000);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
					}
				
				// wait until the summation is done
//					return null;
	}

	public ClientSetup getClientSetup() {
		return clientSetup;
	}

	public void setClientSetup(ClientSetup clientSetup) {
		this.clientSetup = clientSetup;
	}
	

}
